package com.lagou.edu.listener;

import com.lagou.edu.config.RabbitConfig;
import com.lagou.edu.entity.Order;
import com.lagou.edu.enums.OrderStatusEnum;
import com.lagou.edu.repository.OrderRepository;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author 赵虎
 * @date 2021/4/1 12:36 上午
 */
@Component
public class OrderListener {

    @Autowired
    private OrderRepository orderRepository;

    @RabbitListener(queues = RabbitConfig.ORDER_QUEUE_NAME, ackMode = "MANUAL")
    public void orderMessage(Order order , Channel channel , Message message) throws IOException {
        order.setOrderStatus(OrderStatusEnum.TO_BE_PAID.getValue());
        orderRepository.save(order);
        channel.basicAck(message.getMessageProperties().getDeliveryTag() , false);
    }
}
